// pages/index/index.js
const call = require("../../utils/request.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentSort: 0,
    pageNum: 1,
    banner:[
      '/images/banner-1.jpg',
      '/images/banner-2.jpg',
      '/images/banner-3.jpg'
    ],
    jobsorts: [
      {
        id: 'remen',
        name: '全部'
      },
      {
        id: 'canting',
        name: '餐厅'
      },
      {
        id: 'tushuguan',
        name: '图书馆'
      },
      {
        id: 'yuepai',
        name: '约拍'
      },
      {
        id: 'kuaidi',
        name: '快递'
      },
      {
        id: 'huodong',
        name: '活动'
      },
      {
        id: 'qita',
        name: '其他'
      },
    ],
    jobslist: '',
    isLoading: false,
    bottomTip: ''
  },
  changSort: function(e){
    console.log(e)
    const that = this
    const sort = e.detail.index
    const openId = wx.getStorageSync('token');
    this.setData({
      currentSort: sort,
      bottomTip: '',
      isLoading: false
    })
    call.requestGet('jus/findAllBySort', 
      {
        sort: sort,
        pageNum: 1,
        openId: openId
      },
      function (res) {
        console.log(res)
        that.setData({
          jobslist: res,
          bottomTip: '',
          pageNum: 1,
        })
      },
      this.getJoblistFail
    )
  },

  // 请求失败
  getJoblistFail: function (e) {
    console.log("调用接口失败", e)
  },

  // 首页点击收藏事件
  toStore: function(e){
    console.log('tostore',e)
    const openId = wx.getStorageSync('token') || '';
    const that = this
    if(openId){
      const jobId = e.currentTarget.dataset.id
      const jobslist = that.data.jobslist
      for (let i in jobslist){
        // 遍历兼职列表根据id定位点击的兼职
        if (jobId == jobslist[i].id) {
          call.requestGet('jus/goStore', {
            openId: openId,
            id: jobId
          }, function (e) {
            // 请求成功，用这个id定位列表中的那个兼职并更改它的状态
            for (let i in jobslist) {
              if (jobId == jobslist[i].id) {
                console.log("aaa", that.data.jobslist[i].store)
                that.setData({
                  ["jobslist[" + i + "].store"]: !that.data.jobslist[i].store
                })
                console.log("bbb", that.data.jobslist[i].store)
                return
              }
            }
          }, function () {

          });
          return
        }
      }
      
    } else {
      // 弹窗，让用户授权
      wx.navigateTo({
        url: '../authPage/authPage'
      })
    }
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this
    const sort = this.data.currentSort
    const openId = wx.getStorageSync('token');
    call.requestGet('jus/findAllBySort',
      {
        openId: openId,
        sort: sort,
        pageNum: 1
      },
      function (res) {
        console.log(res)
        that.setData({
          jobslist: res
        })
      },
      this.getJoblistFail
    )
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const openId = wx.getStorageSync('token');
    const jobId = wx.getStorageSync('jobId');
    const that = this
    const jobslist = this.data.jobslist
    // 进入详情页会埋下一个所访问的兼职id种子，回到首页，如果有这个id就会发起请求，更新此兼职的状态
    if(openId && jobId){
      call.requestGet('jus/findOne', {
        openId: openId,
        id: jobId
      }, function (e) {
        console.log('jobDetail', e)
        for (let i in jobslist) {
          if (jobId == jobslist[i].id) {
            console.log("id2",jobId)
            that.setData({
              ["jobslist[" + i + "].store"]: e.store
            })
            wx.removeStorageSync('jobId')
            return
          }
        }
      }, function () {

      });
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    const that = this
    const sort = that.data.currentSort
    const openId = wx.getStorageSync('token');
    call.requestGet('jus/findAllBySort',
      {
        openId: openId,
        sort: sort,
        pageNum: 1
      },
      function (res) {
        console.log(res)
        that.setData({
          jobslist: res,
          pageNum: 1
        })
        wx.stopPullDownRefresh();
      },
      this.getJoblistFail
    )
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log("触底了")
    const that = this
    const openId = wx.getStorageSync('token');
    that.setData({
      isLoading: true
    })
    const sort = that.data.currentSort
    const pageNum = ++that.data.pageNum
    call.requestGet('jus/findAllBySort',
      {
        openId: openId,
        sort: sort,
        pageNum: pageNum
      },
      function (res) {
        console.log(res)
        if(res.length){
          that.setData({
            isLoading: false,
            jobslist: that.data.jobslist.concat(res)
          })
        } else {
          that.setData({
            isLoading: false,
            bottomTip: '哎哟，都被你看光了～',
            pageNum: pageNum-1
          })
        }
      },
      function (res) {
        console.log(res)
        that.setData({
          isLoading: false,
          bottomTip: '啊喔，加载失败～'
        })
      }
    )
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})